/**
 * Connection class.
 */
package pits.util;

import java.io.InputStream;
import java.sql.DriverManager;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * @author ltd
 */
public class Connection {
	/**
	 * props property.
	 */
	private static Properties props = new Properties();

	/**
	 * log property.
	 */
	private static Logger log = Logger.getLogger("Connection");
	static {
		try {
			InputStream is = Connection.class.getResourceAsStream("/db.properties");
			props.load(is);
		} catch (Exception ex) {
			// in case occur exception, log it.
			log.error("Load db.properties", ex);
		}
	}

	/**
	 * load the content of db.properties file.
	 * 
	 * @return
	 */
	public static java.sql.Connection getConnection() {
		try {
			String jdbcDriver = props.getProperty(Constants.JDBD_DRIVER);
			String jdbcUrl = props.getProperty(Constants.JDBD_URL);
			String jdbcUser = props.getProperty(Constants.JDBD_USER);
			String jdbcPassword = props.getProperty(Constants.JDBD_PASS);

			Class.forName(jdbcDriver);
			return (java.sql.Connection) DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword);
		} catch (Exception ex) {
			// in case occur exception, log it.
			log.error("getConnection", ex);
		}
		return null;
	}
}
